<html>
<body>
<style>
b { color:#D08; }
tt { color:#080; font-weight:bold; }
h3 i { color:#666; }
ul { list-style-type:lower-alpha; }
li { margin-bottom:5px; }
h1,h2 { text-align:center; }
h4 { font-weight:bold; margin:0px; font-style:italic; }
h4 b { color:#B60; }
body { padding:10px; }
</style>

<!-- ---------------------------------------------------------- Examples --------------------------------------------------------------- -->

<h2>How to run TreeGrid Html examples</h2>

<h3>0) Run and test TreeGrid Html examples <i>(optional)</i></h3>
Download TreeGrid examples package from <a href="http://www.treegrid.com/Download#Trial">http://www.treegrid.com/Download#Trial</a> and unzip it to some directory, preserve its directories structure.<br />
TreeGrid examples package contains simple examples for common JavaScript frameworks located in <b>/ExamplesNode/Html/</b>. <br />
These examples are in pure HTML / JavaScript and don't require any installation.<br />
<br />
<h4><b>Plain examples</b> (without installation and NodeJS)</h4>
<ul>
   <li>
      Copy the whole TreeGrid package to your web server to be accessible by browser.<br />
      Especially the <b>/Grid/</b> and <b>/ExamplesNode/Html/Static/</b> and <b>/ExamplesNode/Html/Dynamic/</b> directories. Preserve the directory structure when copying.<br />
   </li>
   <li>
      Run the <a href="Static/Index.html" target="_blank" class="Local"><b>/ExamplesNode/Html/Static/Index.html</b></a> page from your http server url in your browser. This example creates TreeGrid statically by <b>&lt;treegrid></b> tag.<br />
   </li>
   <li>
      Or run the <a href="Dynamic/Index.html" target="_blank" class="Local"><b>/ExamplesNode/Html/Dynamic/Index.html</b></a> page from your http server url in your browser. This example creates TreeGrid dynamically by JavaScript <b>TreeGrid()</b> function.<br />
   </li>
</ul>

<h4>Optionally install and run sample <b>NodeJS server</b> that serves data for the Html examples from SQLLite database.</h4>
<i>The server is needed for the most of the Html examples to serve their data.</i>
<ul>
   <li>
      Go to directory <b>/ExamplesNode/Server/</b>.
   </li>
   <li>
      Run here command <b>npm install</b> to install the node modules and wait until they are installed.
   </li>
   <li>
      Next run <b>npm start</b> to start the NodeJS server. The server serves at <a href="http://localhost:8000/" target="_blank"><b>http://localhost:8000/</b></a>.
   </li>
</ul>




<br />
<hr />
<br />
<h1>Using TreeGrid in common <b>JavaScript</b> framework</h1>
<br />
<hr />

<!-- ---------------------------------------------------------- Install --------------------------------------------------------------- -->

<h2>Installation</h2>

<h3>1) Copy TreeGrid distribution <i>(/Grid)</i></h3>
Copy TreeGrid required files from TreeGrid examples package to the project. 
The TreeGrid examples package can be downloaded from <a href="http://www.treegrid.com/Download#Trial">http://www.treegrid.com/Download#Trial</a><br />
<ul>
   <li>
      Copy the whole <b>/Grid</b> directory, including all subdirectories, from TreeGrid package to your project.<br />
      The destination location can be chosen freely, it just must be accessible for the server for reading.<br /> 
      <i>In some JavaScript frameworks it must be copied into specific directory that is served statically without processing the files by the framework, often named <b>static</b>, <b>public</b> or <b>assets</b>.</i><br />
      It can be also shared among more projects if the location is accessible for all the project servers.<br />
   </li>
   <li>
      <i>Optionally</i> copy the whole <b>/GridSrc</b> directory, including all subdirectories, from TreeGrid package to your project. <br />
      If you plan to modify TreeGrid css files and want to link the source css files to TreeGrid for simpler debugging.<br />
   </li>
   <li>
      <i>Optionally</i> if you purchased the <b>debug sources</b>, copy the whole TreeGrid debug sources package, including all subdirectories, to your project. <br />
      If you want to include the TreeGrid plain sources instead of compiled GridE.js file for easy debugging.<br />
      <i>Remember, the TreeGrid source files must not be accessible outside your company!</i>.<br />
   </li>
</ul>
If your framework or server requires to specify the directories that are served statically without processing them by the framework or server, 
add the paths to the copied files to the project configuration. <br />
Add here path also to all TreGrid static layout and data JSON / XML files if your project uses them.<br />

<h3>2) Include TreeGrid script <i>(GridE.js)</i></h3>
Link TreeGrid script file <b>GridE.js</b> to the project main html file, e.g. into <b>index.html</b>, into &lt;head> section as <tt>&lt;script src="Grid/GridE.js">&lt;/script></tt>, use correct path to GridE.js file.<br />
Or if your project displays TreeGrid only conditionally, you can link <b>GridED.js</b> short script instead of GridE.js main script to download the GridE.js automatically on demand.<br />

<h3>3) <i>If you use TypeScript</i> copy TreeGrid TypeScript API definition <i>(TreeGrid.TypeScript.API.d.ts)</i></h3>
Copy file <b>TreeGrid.TypeScript.API.d.ts</b> (located in the TreeGrid examples package in <b>/ExamplesNode/Html/</b> directory) to the project sources directory, often named <b>src</b>.<br />
This file contains TypeScript declaration of all TreeGrid API methods, properties and events.<br />
If you use <b>isolatedModules</b>, <b>import</b> the <b>TreeGrid.TypeScript.API.d.ts</b> to all files where the TreeGrid API is used.<br />
<br />
<hr />

<!-- ---------------------------------------------------------- Create --------------------------------------------------------------- -->

<h2>Create TreeGrid grid / Gantt / sheet</h2>

<h3>4) Create TreeGrid statically by HTML tag <i>(&lt;treegrid> / &lt;bdo>)</i></h3>
Create TreeGrid statically somewhere in your page by adding <b>&lt;treegrid></b> tag to some html file or component template or script that generates the page html.<br />
<ul>
   <li>
      Include <b>&lt;treegrid></b> tag in some &lt;div> element that will be used as TreeGrid container (called MainTag). 
      It can be included directly in html code in page or in html code in component template or in script that generates the page html.<br />
      For example add such code <tt>&lt;div style="height:500px;">&lt;treegrid debug='check' layout_url="static/Layouts/StaticDef.js" data_url="static/Layouts/StaticData.js">&lt;/treegrid>&lt;/div></tt>.<br />
   </li>
   <li>
      If your framework does not support custom html tags, use <b>&lt;bdo></b> tag instead of &lt;treegrid> tag. The &lt;bdo> tag is processed by TreeGrid if it has <u>not</u> set its dir attribute.<br />
   </li>
   <li>
      <i>If the html code with &lt;treegrid> / &lt;bdo> tag is not rendered on page start</i>, call <b>StartTreeGrid()</b> global function to process the created &lt;treegrid> / &lt;bdo> tags.<br />
      The StartTreeGrid() must be called after the html code with the tags is rendered to the page, usually in some framework event handler like "mount" or "render".<br />
      The StartTreeGrid() can be called more times without any problem, it processes every tag only once.<br />
   </li>
   <li>
   <i>If used <b>TypeScript</b></i>, you may need to add <b>//@ts-ignore</b> comment line before the &lt;treegrid> / &lt;/bdo> tag definition to suppress TypeScript warning or error for the custom tag.<br />
   </li>
</ul>
For more information about &lt;treegrid> tag see <a href="http://www.treegrid.com/Doc/Create.htm#Creating">Creating TreeGrid</a>.<br />

<h3>5) Create TreeGrid dynamically by script <i>(TreeGrid(...))</i></h3>
Or create TreeGrid dynamically from JavaScript or TypeScript by <b>TreeGrid( )</b> function.<br />
For example: <tt>var grid = TreeGrid({Debug:'check', Layout: { Url:"Layouts/StaticDef.js" }, Data: { Url:"Layouts/StaticData.js" } },"MyTag");</tt>, where MyTag is an id of &lt;div> tag where TreeGrid will be created (it will be TreeGrid MainTag).<br />
For more information about TreeGrid( ) function see <a href="http://www.treegrid.com/Doc/Create.htm#Creating">Creating TreeGrid</a>.<br />
<br />
Existing grid can be deleted by <b>grid.Dispose( )</b> method. For more information see <a href="http://www.treegrid.com/Doc/Create.htm#Deleting">Deleting TreeGrid</a>.<br />
To reload grid with new content use <b>grid.ReloadBody( )</b> method, to recreate grid completely from new layout and data use <b>grid.Reload( )</b> method. For more information see <a href="http://www.treegrid.com/Doc/Create.htm#Reloading">Reloading TreeGrid</a>.<br />
To access existing grid on page by API link the TreeGrid with the framework component in point 6) or use global Grids object as <b>Grids[grid_id]</b> or for the only grid on page use <b>Grids[0]</b>, for example <tt>Grids[0].Reload();</tt>. <br />
For more information see <a href="http://www.treegrid.com/Doc/Create.htm#API">Accessing TreeGrid by API</a>.<br />

<h3>6) Link created TreeGrid with the framework component <i>(for API)</i></h3>
The simplest way is to assign a reference to the framework component in TreeGrid object and assign a reference to TreeGrid object in the framework component.<br />
<ul>
   <li>
      Define some property in the framework component that will contain a reference to the created TreeGrid.<br />
      Name it e.g. <b>Grid</b>. If required, initialize it to null or undefined.<br />
   </li>
   <li>
      <i>If TreeGrid is created statically by &lt;treegrid> / &lt;bdo> tag</i>, do the assignment in constructor or in some event handler of the framework component, something like create, init or mount.<br />
      For example: <tt>... oncreate: function () { ... var Component = this; Grids.OnInit = function(grid){ grid.Component = Component; Component.Grid = grid; }; ... }, ...</tt><br />
      The Grids.OnInit can be used only if there is only one TreeGrid on page. 
      For more grids on page assign the event only for particular grid as: <tt>... TGSetEvent("OnInit",grid_id,function(grid){ grid.Component = Component; Component.Grid = grid; });</tt><br />
      In TreeGrid versions prior 14.1 use OnLoaded event instead of OnInit.<br />
   </li>
   <li>
      <i>If TreeGrid is created dynamically by TreeGrid() function</i>, you can do the assignment directly on TreeGrid() function call. <i>In TreeGrid versions prior 14.1 use the previous way.</i><br />
      For example: <tt><b>this.Grid</b> = TreeGrid({Debug:'check', Layout: { Url:"Layouts/StaticDef.js" }, Data: { Url:"Layouts/StaticData.js" } },"MyTag",<b>{Component:this}</b>);</tt>.<br />
   </li>
   <li>
   After that, the TreeGrid will be accessible from the framework component as <b>this.Grid</b> 
   and the framework component will be accessible from the TreeGrid event handlers, formulas and actions as <b>Grid.Component</b>.
   </li>
</ul>

<br />
<hr />
<!-- ---------------------------------------------------------- Data --------------------------------------------------------------- -->

<h2>Create TreeGrid data (structure and content)</h2>

<h3>7) TreeGrid data formats <i>(JSON / XML)</i></h3>
<ul>
   <li><b>Download data format</b><br />
      TreeGrid creates the grid from data in XML or JSON format. This format is proprietary for TreeGrid. The XML/JSON data contains all required definitions: configuration, columns, rows, cells, values, etc.<br />
      You need to create this XML/JSON data and pass it to TreeGrid. How this XML/JSON data is created depends fully on you. It can be created statically in some file or dynamically by some server or client script.<br />
      <i>In this package there is NodeJS server with sample JavaScript code in <b>Server/index.js</b> that creates the JSON data for TreeGrid from SQLLite database or serves static file.</i><br />
      For more information about the TreeGrid XML/JSON data format for download see <a href="http://www.treegrid.com/Doc/DataDownload.htm#LayoutXML">Layout format</a> and <a href="http://www.treegrid.com/Doc/DataDownload.htm#DataXML">Data format</a>.
      The documentation is for XML, the JSON format has the same tags, just different structure, see <a href="http://www.treegrid.com/Doc/DataFormats.htm#JSONFormat">JSON format</a><br />
   </li>
   <li><b>Upload data format</b><br />
      TreeGrid uploads the changes to server in XML or JSON format, similar to download format. See documentation <a href="http://www.treegrid.com/Doc/DataUpload.htm#XML">Upload format</a><br />
      TreeGrid can upload only changes (added/deleted/moved/changed rows and their cells) or the whole data (all rows or even all data), it is controlled <a href="http://www.treegrid.com/Doc/DataUpload.htm#Upload_Type">Upload_Type</a>.<br />
      The server should parse this uploaded data and save it to its data source (database, file, etc.). This server side code is created you. <br />
      <i>In this package there is NodeJS server with sample JavaScript code in <b>Server/index.js</b> that parses uploaded TreeGrid JSON data and saves the changes to SQLLite database or to static file.</i><br />
   </li>
</ul>

<h3>8) Defining TreeGrid layout <i>(columns and configuration)</i></h3>
TreeGrid layout defines the grid / Gantt structure: configuration, columns (types, formats, widths, ...), toolbars, control rows, Gantt definition, etc.
Simply it contains the whole TreeGrid definition except body rows and their cells. See <a href="http://www.treegrid.com/Doc/DataDownload.htm#LayoutXML">Layout format</a> and <a href="http://www.treegrid.com/Doc/DataFormats.htm#JSONFormat">JSON format</a>.<br />
TreeGrid layout is usually static JSON or XML file, usually located in <b>public</b>, <b>static</b>, <b>assets</b> or similar directory accessible directly for browser.<br />
But TreeGrid layout can be also dynamically created on server or by API or merged to TreeGrid data.<br />
The layout definition is assigned in Layout object in TreeGrid() method (e.g. <tt>TreeGrid({Layout:{Url:'test.json'},...},...)</tt>) or with Layout_ prefix in &lt;treegrid> tag (e.g. <tt>&lt;treegrid Layout_Url='test.json' ... ></tt>).<br />
For more information about the TreeGrid layouts see especially the <b>Tutorial</b> examples in this package that describe individual features and how defined and use them.<br />

<h3>9) Creating TreeGrid data <i>(rows and cells)</i></h3>
TreeGrid data defines the grid data rows and their cells with values. see <a href="http://www.treegrid.com/Doc/DataDownload.htm#DataXML">Data format</a> and <a href="http://www.treegrid.com/Doc/DataFormats.htm#JSONFormat">JSON format</a>.<br />
TreeGrid data is usually created dynamically on server side from database or any other data source.<br />
But TreeGrid data can be also static JSON / XML file or it can be generated dynamically by API.<br />
The data definition is assigned in Data object in TreeGrid() method (e.g. <tt>TreeGrid({Data:{Url:'testdata.json'},...},...)</tt>) or with Data_ prefix in &lt;treegrid> tag (e.g. <tt>&lt;treegrid Data_Url='testdata.json' ... ></tt>).<br />

<h3>10) TreeGrid data communication <i>(AJAX / REST)</i></h3>
There are three usual ways of passing data to TreeGrid.<br />
<ul>
   <li><b>Built-in AJAX communication</b><br />
      By default TreeGrid loads its data from server and uploads changes back to server by built-in AJAX.<br />
      You just specify the urls where to download and upload and their parameters and TreeGrid communicates directly with server.<br />
      For example <tt>&lt;treegrid Layout_Url='Layouts/TableDef.js' Data_Url='http://localhost:8000/get?table=TableData&idcol=ID' Upload_Url='http://localhost:8000/set?table=TableData&idcol=ID' Upload_Format='JSON' Upload_Data='Data'></tt><br />
      or <tt>TreeGrid({ Layout:{Url:'Layouts/TableDef.js'}, Data:{Url:'http://localhost:8000/get?table=TableData&idcol=ID'}, Upload:{Url:'http://localhost:8000/set?table=TableData&idcol=ID', Format:'JSON', Data:'Data'},"MyTag");</tt><br />
      For more information about the default AJAX communication see <a href="http://www.treegrid.com/Doc/DataCommunication.htm#AJAX">AJAX communication</a>.<br />
   </li>
   <li><b>Custom communication</b><br />
      For custom communication (AJAX or another type) define the TreeGrid data sources in the same way as in the previous point a.<br />
      And define API event <b>OnCustomAjax</b>. In this event you get all the required information for the communication. Start the custom communication and return true. And after the communication finished call the provided callback function with the result.<br />
      For example <tt>Grids.OnCustomAjax = function(G,IO,data,func){ MyAjax(IO.Url,data,function(result){ if(result<0) func(result,"Error"); else func(0,result); }); return true; }</tt>
      For more information see <a href="http://www.treegrid.com/Doc/DataCommunication.htm#OnCustomAjax">OnCustomAjax</a>.<br />
   </li>
   <li><b>Direct data</b><br />
      It is possible to create TreeGrid from already loaded or created data.<br />
      Pass the data to <b>Data</b> parameter as string or JSON object or to <b>Script</b> parameter as global JavaScript variable name.<br />
      For example you can define <tt>MyObject = { MyData: {Cols:[{Name:'A'},{Name:'B'}]} }; </tt> <br />
      and <tt>TreeGrid({Layout:{Script:"MyObject.MyData"},Data:{Data:{Body:[[{id:1,A:10,B:20},{id:2,A:20,B:40}]]}}},"MyTag");</tt><br />
      or <tt>&lt;treegrid Layout_Script:"MyObject.MyData" Data_Data="{Body:[[{id:1,A:10,B:20},{id:2,A:20,B:40}]]}"></tt><br />
      For more information see <a href="http://www.treegrid.com/Doc/DataCommunication.htm#Data">Directly included data</a> and <a href="Data from JavaScript">Data from JavaScript</a>.<br />
   </li>
</ul>

<br />
<hr />

<!-- ---------------------------------------------------------- Events --------------------------------------------------------------- -->

<h2>TreeGrid events and API</h2>

<h3>11) Catch TreeGrid XML/JSON events <i>(Actions in data)</i></h3>
The XML/JSON event handlers are defined in TreeGrid Defaults.xml (located in /Grid directory), in layout XML/JSON file or data or in another input XML/JSON data.<br />
The XML/JSON events are mostly mouse and key events like OnClick or OnMouseMove. There are also a few special events like OnChange.<br />
The handlers are defined by <b>On...</b> attribute in <b>Actions</b> tag for the whole grid or by <b>On...</b> attribute assigned to particular column, default column, row, default row, row cell, default row cell.<br />
The handlers are defined as JavaScript code in string. For example <tt>Actions: { ... OnClick:"alert('Clicked cell '+Row.id+','+Col);return 1;" ... }</tt> or <tt>Body:[[{... Col1OnClick:"alert('Clicked cell '+Row.id+','+Col);return 1;" ...},...]]</tt><br />
The framework component (linked in point 6)) can be accessed in the handler code by <b>Grid.Component</b>, for example <tt>Actions: { ... OnClick:"Grid.Component.MyProcessClick(Row,Col)" ... }</tt><br />
The handler should return 1 to cancel event propagation or 0 to continue propagation and run next event handlers.<br />
For more information see <a href="http://www.treegrid.com/Doc/Events.htm#Mouse">TreeGrid mouse events</a> and <a href="http://www.treegrid.com/Doc/Events.htm#Key">TreeGrid key events</a>.<br />

<h3>12) Catch TreeGrid API events <i>(in script)</i></h3>
TreeGrid API event handlers are JavaScript callbacks, they are defined as standard JavaScript / TypeScript functions that are called from TreeGrid code when the API event happens.<br />
The API events can be used to run some code after TreeGrid is loaded, ready or rendered or some action is started or finished like data communication, editing, sorting, filtering, mouse actions like click, etc.<br />
The event handlers can be assigned before any TreeGrid is created, in global code or in constructor or in some early event (like create, init, mount,etc.) of framework component that uses TreeGrid<br />
For example: <tt>... oncreate: function () { ... Grids.OnReady = function(G){ alert(G.id+" grid is ready to render"); } ... }, ...</tt><br />
For more information about assigning API events see <a href="http://www.treegrid.com/Doc/Events.htm#API">TreeGrid API events</a>.<br />

<h3>13) Using TreeGrid API <i>(API methods)</i></h3>
The most of TreeGrid API methods are methods of (TGrid) grid object. 
The grid object can be got by <b>this.Grid</b> if called in the framework component linked with created TreeGrid as described in point 6).<br />
Or grid object can be got by global <b>Grids</b> object as <b>Grids[grid_id]</b> or for the only grid on page use <b>Grids[0]</b>, for example <tt>Grids[0].Reload();</tt>.
For more information see <a href="http://www.treegrid.com/Doc/Create.htm#API">Accessing TreeGrid by API</a>.<br />
Many TreeGrid methods require (TRow) row object. The row object can be got by its id by <a href="http://www.treegrid.com/Doc/RowAPI.htm#GetRowById">GetRowById</a> API method or by iterating rows, see <a href="www.treegrid.com/Doc/RowAPI.htm#Iteration">Row iteration</a>.<br />
The column are passed to TreeGrid methods by their names as strings. The columns can be also iterated see <a href="http://www.treegrid.com/Doc/ColAPI.htm">Column iteration</a>.<br />

<script> if(location.protocol.search(/http/i)<0 || location.pathname.search(/examplesnode/i)<0) for(var i=0,AE=document.getElementsByClassName("Local");i<AE.length;i++) AE[i].removeAttribute("href");  </script>

</body>
</html>